﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Text;


namespace IHISCETeim.Web.MachineSys.FXModel
{
    public partial class ReportFXAgentYuQi : PageBase
    {
        protected override string DWZRef
        {
            get
            {
                return "ReportFXAgentYuQi";
            }
        }

        protected int ReportType
        {
            get
            {
                return GetRequestValue<int>("rt", 1);
            }
        }

        protected short YearTag
        {
            get
            {
                return Convert.ToInt16(DateTime.Now.Year);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.AddHeader("pragma", "no-cache");
            Response.CacheControl = "no-cache";

            switch (ReportType)
            {
                case 1:
                    DataBind1();
                    break;
                case 2:
                    DataBind2();
                    break;
                case 3:
                    DataBind3();
                    break;                
            }
        }

        #region 1.经销商逾期率
        private void DataBind1()
        {
            this.gv1_1.DataSource = FXAgentYuQiController.GetList(FXAgentYuQiExpiryType.经销商明细.ToString(), YearTag);
            this.gv1_1.DataBind();

            this.gv1_2.DataSource = FXAgentYuQiController.GetList(FXAgentYuQiExpiryType.经销商明细.ToString(), (short)(YearTag-1));
            this.gv1_2.DataBind();
        }

        #endregion

        #region 2.整体逾期率趋势


        private DataTable _report2dts = null;
        private DataTable report2dts
        {
            get
            {
                if (_report2dts == null)
                {

                    _report2dts = FXAgentYuQiController.GetList(FXAgentYuQiExpiryType.年度汇总.ToString(),0);
                }
                return _report2dts;
            }
        }

        private void DataBind2()
        {
            this.gv2.DataSource = report2dts;
            this.gv2.DataBind();
        }

        protected string GetReport2JSCategories()
        {
            DataTable dtcl = report2dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int colindex = 1;
            foreach (DataColumn dc in dtcl.Columns)
            {
                if (dc.ColumnName == "年份" || dc.ColumnName == "简称" || dc.ColumnName == "经销商全称")
                    continue;
                sb.AppendFormat("'{0}'", dc.ColumnName);
                if (colindex != dtcl.Columns.Count - 3)
                {
                    sb.AppendFormat(",");
                }
                colindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport2JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report2dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            string str直接 = "";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 = "{name: '" + dr["年份"].ToString() + "',data:[";
                //int cNum = 1;
                //foreach (DataColumn dc in dtmain.Columns)
                //{
                //    if (dc.ColumnName == "年份" || dc.ColumnName == "简称" || dc.ColumnName == "经销商全称")
                //        continue;
                //    str直接 += Convert.ToDecimal(dr[dc.ColumnName]).ToString("f2");
                //    if (cNum != dtmain.Columns.Count - 3)
                //    {
                //        str直接 += ",";
                //    }
                //    cNum++;
                //}
                int cNum = 0;
                foreach (DataColumn dc in dtmain.Columns)
                {
                    if (dc.ColumnName == "年份" || dc.ColumnName == "简称" || dc.ColumnName == "经销商全称")
                        continue;
                    if (!(dr[dc.ColumnName] is DBNull))
                    {
                        str直接 += string.Format("[{0},{1}]", cNum, dr[dc.ColumnName]);
                        if (cNum != dtmain.Columns.Count - 4)
                        {
                            str直接 += ",";
                        }
                    }
                    cNum++;
                }
                //去掉最后一个逗号
                string lastChar = str直接.Substring(str直接.Length - 1, 1);
                if (lastChar == ",")
                    str直接 = str直接.Substring(0, str直接.Length - 1);



                str直接 += "]}";
                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                sb.AppendLine(str直接);
                rowindex++;
            }

            sb.AppendLine("]");
            return sb.ToString();
        }

        #endregion

        #region 3.整体逾期率趋势

        protected string topagentList
        {
            get
            {
                return GetRequestValue<string>("topagentList", topagentreport3dts.Rows[0]["经销商"].ToString());
            }
        }


        protected string GettopagentList()
        {
            //htmlr = string.Format("<select name=\"{0}\" class='combox'>", controlName);
            string htmlr = "";// string.Format("<option value=\"{0}\" {1}>{2}</option>", "", "" == topagentList ? "selected" : "", "请选择...");
            foreach (DataRow item1 in topagentreport3dts.Rows)
            {
                htmlr += string.Format("<option value=\"{0}\" {1}>{0}({2}台)</option>", item1["经销商"], item1["经销商"].ToString() == topagentList ? "selected" : "", item1["合计"].ToString());
            }


            return htmlr;
        }

        protected string GetTopAgentUL()
        {
            string htmlr = "";// string.Format("<option value=\"{0}\" {1}>{2}</option>", "", "" == topagentList ? "selected" : "", "请选择...");
            foreach (DataRow item1 in topagentreport3dts.Rows)
            {
                htmlr += string.Format("<li class='{0}'><a href=\"ReportFXAgentYuQi.aspx?rt=3&topagentList={1}\">{2}</a></li>", item1["经销商"].ToString() == topagentList ? "selected" : "", HttpUtility.UrlEncode(item1["经销商"].ToString()), item1["经销商"]);
            }
            return htmlr;
        }

        #region
        private int topRow=9;//2012年重点经销商为前9个
        private DataTable _topagentreport3dts = null;
        private DataTable topagentreport3dts
        {
            get
            {
                if (_topagentreport3dts == null)
                {
                    _topagentreport3dts = FXFinanceLeaseController.ReportGetJXSFKTS();

                    int oldcolumnNum = _topagentreport3dts.Columns.Count;
                    _topagentreport3dts.Columns.Add("合计", typeof(int));
                    int row合计 = 0;
                   
                    foreach (DataRow dr in _topagentreport3dts.Rows)
                    {
                        row合计 = 0;
                        for (int cIndex = 1; cIndex < oldcolumnNum; cIndex++)
                        {
                            row合计 += Convert.ToInt32(dr[cIndex]);
                        }
                        dr["合计"] = row合计;
                        
                        
                    }
                    //
                    DataTable dt = _topagentreport3dts.Copy();
                    DataView dv = dt.DefaultView;
                    dv.Sort = " 合计 desc";
                    _topagentreport3dts = dv.ToTable();
                    dt.Rows.Clear();
                    //DataTable reDT = _topagentreport3dts.Copy();
                    int allRow = _topagentreport3dts.Rows.Count;
                    for (int rowIndex = allRow - 1; rowIndex >= topRow; rowIndex--)
                    {
                        topagentreport3dts.Rows[rowIndex].Delete();
                       
                    }
                  

                }
                return _topagentreport3dts;
            }
        }
        #endregion

        private DataTable _report3dts = null;
        private DataTable report3dts
        {
            get
            {
                if (_report3dts == null)
                {

                    _report3dts = FXAgentYuQiController.ReportGetDetail(topagentList);
                }
                return _report3dts;
            }
        }

        private void DataBind3()
        {
            this.gv3.DataSource = report3dts;
            this.gv3.DataBind();
        }

        protected string GetReport3JSCategories()
        {
            DataTable dtcl = report3dts;
            StringBuilder sb = new StringBuilder();
            //表头添加
            sb.Append("[");
            int colindex = 1;
            foreach (DataColumn dc in dtcl.Columns)
            {
                if (dc.ColumnName == "年份" || dc.ColumnName == "简称" || dc.ColumnName == "经销商全称")
                    continue;
                sb.AppendFormat("'{0}'", dc.ColumnName);
                if (colindex != dtcl.Columns.Count - 3)
                {
                    sb.AppendFormat(",");
                }
                colindex++;
            }
            sb.Append("]");
            string filedstr = sb.ToString();
            return filedstr;
        }

        protected string GetReport3JSSeries()
        {
            /*
             [{
                    name: 'John',
                    data: [5, 3, 4, 7, 2]
                }, {
                    name: 'Jane',
                    data: [2, 2, 3, 2, 1]
                }, {
                    name: 'Joe',
                    data: [3, 4, 4, 2, 5]
                }]
             */
            DataTable dtmain = report3dts;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("[");
            string str直接 = "";
            int rowindex = 1;
            foreach (DataRow dr in dtmain.Rows)
            {
                str直接 = "{name: '" + dr["年份"].ToString() + "',data:[";
                //int cNum = 1;
                //foreach (DataColumn dc in dtmain.Columns)
                //{
                //    if (dc.ColumnName == "年份" || dc.ColumnName == "简称" || dc.ColumnName == "经销商全称")
                //        continue;
                //    str直接 += Convert.ToDecimal(dr[dc.ColumnName]).ToString("f2");
                //    if (cNum != dtmain.Columns.Count - 3)
                //    {
                //        str直接 += ",";
                //    }
                //    cNum++;
                //}
                int cNum = 0;
                foreach (DataColumn dc in dtmain.Columns)
                {
                    if (dc.ColumnName == "年份" || dc.ColumnName == "简称" || dc.ColumnName == "经销商全称")
                        continue;
                    if (!(dr[dc.ColumnName] is DBNull))
                    {
                        str直接 += string.Format("[{0},{1}]", cNum, dr[dc.ColumnName]);
                        if (cNum != dtmain.Columns.Count - 4)
                        {
                            str直接 += ",";
                        }
                    }
                    cNum++;
                }
                //去掉最后一个逗号
                string lastChar = str直接.Substring(str直接.Length - 1, 1);
                if (lastChar == ",")
                    str直接 = str直接.Substring(0, str直接.Length - 1);


                str直接 += "]}";
                if (rowindex != dtmain.Rows.Count)
                {
                    str直接 += ",";
                }
                sb.AppendLine(str直接);
                rowindex++;
            }

            sb.AppendLine("]");
            return sb.ToString();
        }

        #endregion
    }
}